package com.example.combwyaq.mvp.presenter;


import android.util.Log;

import com.example.combwyaq.App;
import com.example.combwyaq.db.DaoManager;
import com.example.combwyaq.entity.ResponseUserEntity;
import com.example.combwyaq.mvp.contract.LoginConteract;
import com.example.combwyaq.mvp.model.entity.BaseEntity;
import com.example.combwyaq.mvp.model.entity.LoginEntity;
import com.example.combwyaq.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoginPresenter extends BasePresenter<LoginConteract.ILoginModel,LoginConteract.ILoginView> {
    @Inject
    public LoginPresenter(LoginConteract.ILoginModel iLoginModel, LoginConteract.ILoginView iLoginView) {
        super(iLoginModel, iLoginView);
    }

    //声明对应请求方法
    //phone->当前用户登陆的手机号
    //pwd->用户密码
    public  void requestLogin(long phone ,String pwd,int code){
        if (pwd.isEmpty()){
            ToastUtils.showMsg("当前密码为空");
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("phone",phone);
        map.put("pwd",pwd);
        map.put("code",code);
        m.request(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    public void onNext(BaseEntity entity) {
        Log.i("qqq", "onNext: ");
        if (entity instanceof ResponseUserEntity){
            //登陆或者注册返回->保存到本地操作
            ResponseUserEntity responseUserEntity= (ResponseUserEntity) entity;
            try {
                DaoManager.getInstance().getDaoSession().insert(responseUserEntity.getValues());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        v.refreshView(entity);
    }
}
